package com.xiaohe66.web.gateway.http.task;

import com.xiaohe66.common.value.R;
import com.xiaohe66.web.application.task.TaskAppService;
import com.xiaohe66.web.application.task.bo.TaskChangePoolBo;
import com.xiaohe66.web.application.task.bo.TaskListBo;
import com.xiaohe66.web.application.task.bo.TaskSaveBo;
import com.xiaohe66.web.application.task.bo.TaskSortBo;
import com.xiaohe66.web.application.task.result.TaskDetailResult;
import com.xiaohe66.web.application.task.result.TaskResult;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author xiaohe
 * @since 2021.11.17 14:19
 */
@RestController
@RequestMapping("/task")
@Slf4j
@RequiredArgsConstructor
public class TaskController {

    private final TaskAppService appService;

    /**
     * 保存, 不存在时新增，存在时更新，统一返回id
     */
    @PostMapping
    public R<Long> save(@RequestBody TaskSaveBo bo) {

        return R.ok(appService.save(bo));
    }

    @DeleteMapping("/{id}")
    public R<Void> remove(@PathVariable Long id) {

        appService.remove(id);
        return R.ok();
    }

    @PostMapping("/changePool")
    public R<Void> changePool(@RequestBody TaskChangePoolBo changePoolBo) {

        appService.changeStatus(changePoolBo);
        return R.ok();
    }

    @PostMapping("/sort")
    public R<Void> sort(@RequestBody TaskSortBo bo) {
        appService.sort(bo);
        return R.ok();
    }

    @GetMapping("/lists")
    public R<List<List<TaskResult>>> lists() {
        return R.ok(appService.queryLists());
    }

    @GetMapping
    public R<List<TaskResult>> list(TaskListBo bo) {
        return R.ok(appService.queryList(bo));
    }

    @GetMapping("/{id}")
    public R<TaskDetailResult> detail(@PathVariable Long id) {
        return R.ok(appService.queryDetail(id));
    }
}
